<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
error_reporting(E_ALL ^ E_NOTICE);
// 应用公共文件
function pr ($data) {
    echo '<pre>';
    print_r($data);
    echo '</pre>';
}

function _pr($data) {
    pr($data);
    exit;
}

/* 生成密码 */
function createPassword ($password) {
    $string = $password . config('authkey');
    return md5(md5($string));
}

/**
 * 验证手机号码
 */
function checkMobile($mobile) {
    return preg_match("/^1[3|4|5|6|7|8|9][0-9]\d{8}$/", $mobile);
}


/* 密码强度 */
function checkPassword ($str) {
    $score = 0;
    if(preg_match("/[0-9]+/",$str))
    {
        $score ++; 
    }
    if(preg_match("/[0-9]{3,}/",$str))
    {
        $score ++; 
    }
    if(preg_match("/[a-z]+/",$str))
    {
        $score ++; 
    }
    if(preg_match("/[a-z]{3,}/",$str))
    {
        $score ++; 
    }
    if(preg_match("/[A-Z]+/",$str))
    {
        $score ++; 
    }
    if(preg_match("/[A-Z]{3,}/",$str))
    {
        $score ++; 
    }
    if(preg_match("/[_|\-|+|=|*|!|@|#|$|%|^|&|(|)]+/",$str))
    {
        $score += 2; 
    }
    if(preg_match("/[_|\-|+|=|*|!|@|#|$|%|^|&|(|)]{3,}/",$str))
    {
        $score ++ ; 
    }
    if(strlen($str) >= 10)
    {
        $score ++; 
    }
    return $score;
}

/* 返回数据组装 */
function returnJson ($code,$message='',$data=[]) {
    return json([
        'code' => $code,
        'message' => $message,
        'data' => $data
    ]);
}

/* 相对路径转全路径 */
function tomedia ($path) {
    if(strstr($path,'http://') || strstr($path,'https://')) {
        return $path;
    }
    $domain = config('qiniu_domain');
    return $domain.$path;
}

function getDomain () {
    return isHttps()?'https://':'http://'.$_SERVER['SERVER_NAME'];
}

function isHttps() {
    if ( !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
        return true;
    } elseif ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) {
        return true;
    } elseif ( !empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
        return true;
    }
    return false;
}

function defaultPage(){
    $page=input('post.page');
    if($page<=0){
        $page=1;
    }
    return $page;
}

//生成订单号
function create_order_sn ($uid,$prefix='SHOP') {
    return $prefix.date("YmdHis").sprintf("%05d", $uid).rand(10,99);
}

/*
* 获取http://域名
*/
function get_host(){
    return $this_url =$_SERVER['HTTP_HOST'];
}


/**
 * 异步调用方法
 * @param $host
 * @param $path
 * @param array $param
 */
function doRequest($host,$path, $param=array())
{
    $query = isset($param) ? http_build_query($param) : '';

    $port = 80;
    $errno = 0;
    $errstr = '';
    $timeout = 10;

    $fp = fsockopen($host, $port, $errno, $errstr, $timeout);

    $out = "POST " . $path . " HTTP/1.1\r\n";
    $out .= "host:" . $host . "\r\n";
    $out .= "content-length:" . strlen($query) . "\r\n";
    $out .= "content-type:application/x-www-form-urlencoded\r\n";
    $out .= "connection:close\r\n\r\n";
    $out .= $query;
    $re=fputs($fp, $out);
    fclose($fp);
}